﻿using System;
using UnityEngine;
using UnityEngine.UI;

#if UNITY_EDITOR
using UnityEditor;
#endif

namespace Mars.MeshUI
{
    [AddComponentMenu("UI/Img", 10)]
    [DisallowMultipleComponent]
    public class Img : Image
    {
        [SerializeField] public eCustomMatAddType matAddType = eCustomMatAddType.nor;


        public SpriteMaskInteraction maskInteraction
        {
            set
            {
                switch (value)
                {
                    case SpriteMaskInteraction.None:
                    {
                        matAddType &= ~(eCustomMatAddType.maskIn | eCustomMatAddType.maskOut);
                    }
                        break;
                    case SpriteMaskInteraction.VisibleInsideMask:
                    {
                        matAddType |= eCustomMatAddType.maskIn;
                        matAddType &= ~(eCustomMatAddType.maskOut);
                    }
                        break;
                    case SpriteMaskInteraction.VisibleOutsideMask:
                    {
                        matAddType |= eCustomMatAddType.maskOut;
                        matAddType &= ~(eCustomMatAddType.maskIn);
                    }
                        break;
                }
            }
        }


        public void SetSprite(Sprite sp)
        {
            sprite = sp;
        }

#if UNITY_EDITOR
        private const string cSpriteAtlasMaterial = "Assets/Common/SpriteAtlasMaterial.mat";
        [NonSerialized] private const string cDefaultMaterialName = "Default UI Material";
        public void UpdateAtlasMaterial()
        {
            if (Application.isPlaying)
            {
                return;
            }

            if (sprite != null)
            {
                if (m_Material == null || m_Material.name == cDefaultMaterialName)
                {
                    m_Material = AssetDatabase.LoadAssetAtPath<Material>(cSpriteAtlasMaterial);
                }
            }
            else
            {
                m_Material = null;
            }
        }
#endif

        protected override void Start()
        {
            var cachedCanvas = canvas;
            if (null != cachedCanvas &&
                (cachedCanvas.additionalShaderChannels & AdditionalCanvasShaderChannels.TexCoord1) == 0)
            {
                cachedCanvas.additionalShaderChannels |= AdditionalCanvasShaderChannels.TexCoord1;
            }

            base.Start();
        }

        public override void Rebuild(CanvasUpdate update)
        {
            // 在现有的Rebuild循环机制下，可以减少一次后续逻辑调用
            if (update != CanvasUpdate.PreRender) return;

#if UNITY_EDITOR
            UpdateAtlasMaterial();
#endif

            base.Rebuild(update);
        }


        public override void CrossFadeColor(Color targetColor, float duration, bool ignoreTimeScale, bool useAlpha,
            bool useRGB)
        {
            //已经是灰态没有意义了
            if (!Color.black.Equals(canvasRenderer.GetColor()))
            {
                base.CrossFadeColor(targetColor, duration, ignoreTimeScale, useAlpha, useRGB);
            }
        }
    }

    public enum eCustomMatAddType
    {
        nor = 1 << 0,
        cure = 1 << 1,
        hsv = 1 << 2,
        both = 1 << 1 | 1 << 2,

        //sprite mask
        maskIn = 1 << 3,
        maskOut = 1 << 4,
    }
}